import common from "../../../utils/public.js";
const db=wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {

    administrator:false,
    caballero:false,
    user:false,
    caballeross:"",
    datalist0:[],
    noworder:true,
    search:"",
    nowder:true,
    trash:"cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/atloal-image/trash.png"
  },


   //点击了搜索按钮
   sea(){
    wx.showLoading({
      title: '数据查询中...',
    })
    this.se(this.data.search,this.data.caballeross)
    wx.hideLoading()
  },
  //获得页面快递设置

  getexpress(caballero,state='出错',page=0,num=5){
    var that=this
    db.collection('express')
    .where({
      caballero:caballero,
      state:state
    })
    .skip(page).limit(num).get().then(res=>{
      if(res.data.length==0&&that.data.datalist0.length==0){
        that.setData({
          noworder:false
        })
      }else if(res.data.length==0){
        wx.showToast({
          title: '已无再多',
          icon:'none',
          duration:2000
        })
      }else{
        var oldlist=that.data.datalist0
        var newDate=oldlist.concat(res.data)
        newDate.forEach(item=>{
          if(typeof(item.time)=="number"){
            item.time=common.getMyData(item.time,"Y-m-d H:i:s")
          }
        })
        that.setData({
          datalist0:newDate
        })
      }
    })
  },

  //用户端获得用户设置
  getuserexpress(caballero,state='出错'){
    var that=this
    db.collection('express').where({
      _openid:caballero,
      state:state
    }).get().then(res=>{
      if(res.data.length==0){
        that.setData({
          nowder:false
        })
      }else{
        that.setData({
          datalist0:res.data
        })
      }
    })

  },

   //获取搜索框中的值
   search(res){
    var that=this
    that.setData({
      search:res.detail.value
    })
  },

    //模糊查询
    se(value,caballero){
      var that = this
      db.collection('express').where(_.or([
        {
        //使用正则查询，实现对搜索的模糊查询
        useradd: db.RegExp({
          regexp: value,
          //从搜索栏中获取的value作为规则进行匹配。
          options: 'i',
          //大小写不区分
        })
      },{
        expressadd: db.RegExp({
          regexp: value,
          options: 'i',
        })
      },{
        pickupnum: db.RegExp({
          regexp: value,
          options: 'i',
        })
      }
      ]).and([
        {
          caballero:caballero
        },{
          state:'出错'
        }
      ])).get({
        success: res => {
          if(res.data.length==0){
            wx.showToast({
              title: '查无此数据',
              icon:"none",
              duration:2000
            })
          }
          else{
             that.setData({
             datalist0: res.data
          })
          }
        }
      })
  },

  noworderBody(res){
    var id=res.currentTarget.dataset.id
    var idx=res.currentTarget.dataset.idx
    wx.navigateTo({
      url: '/pages/login/administrator-exprssorder/exprss-order/exprss-order?expressId='+id,
    })
  },

  //测试打开的用户类型
  texttype(){
    var that=this
    wx.cloud.callFunction({
      name:'login',
      success(res){
        let openid=res.result.openid
        that.setData({
          caballeross:res.result.openid
        })
        db.collection('user').where({
          openid:openid
        }).get().then(ress=>{
          let property=ress.data[0].property
          if(property=="骑手"){
            that.setData({
              administrator:false,
              caballero:true,
              user:false
            })
            that.getexpress(that.data.caballeross)
          }else if(property=="用户"){
            that.setData({
              administrator:false,
              caballero:false,
              user:true
            })
            that.getuserexpress(that.data.caballeross)
          }
          
        })
      }
    })
  },


  // 点击图标
  clickRow(res){

    var that=this
    let id=res.currentTarget.dataset.id
    let idx=res.currentTarget.dataset.idx
    wx.showModal({
      title: '提示',
      content: '是否确定删除该错误订单',
      success (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '数据删除中...',
            mask:true
          })
          db.collection('express').doc(id).get().then(res=>{
            console.log(res.data.image)
            // 删除图片
            let fileid=res.data.image
            wx.cloud.callFunction({
              name:'deleteimage',
              data:{
                fileid:fileid
              },
              success(rex){
                wx.cloud.callFunction({
                  name:'expressdelete',
                  data:{
                    id:id
                  },
                  success(xx){
                    var data=that.data.datalist0
                    data.splice(idx,1)
                    that.setData({
                      datalist0:data
                    })
                    wx.hideLoading({
                      success: (res) => {
                        wx.showToast({
                          title: '删除成功',
                          icon:'success',
                          datalist0:2000
                        })
                      },
                    })
                  }
                })
              }
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    that.texttype()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    var that=this
    wx.showLoading({
      title: '数据加载中...',
    })
    var page=that.data.datalist0.length
    that.getexpress(that.data.caballeross,'出错',page,5)
    wx.hideLoading()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})